package com.haohuo.interceptor;

import com.haohuo.config.constans.TraceConstans;
import com.haohuo.util.TraceIDUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

/**
 * feign远程调用记录traceId
 *
 * @Author: pk.zhang
 * @Date: 2021/3/10 10:04
 */
public class FeignTraceInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String traceId = MDC.get(TraceConstans.TRACEID_KEY);
        if (StringUtils.isEmpty(traceId)) {
            traceId = String.valueOf(TraceIDUtil.incrementAndGet());
        }
        requestTemplate.header(TraceConstans.TRACEID_KEY, traceId);
    }


    /*@ConditionalOnClass(Feign.class)
    public static class FeignTraceAutoConfiguration {

        @Bean
        public FeignInterceptor feignTraceInterceptor() {
            return new FeignInterceptor();
        }
    }*/
}
